package com.adams.app.ui;

import java.util.Locale;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v7.app.ActionBarActivity;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.adams.app.R;
import com.adams.app.data.RequestManager;
import com.adams.app.vendor.HappyGyApi;
import com.manuelpeinado.fadingactionbar.FadingActionBarHelper;

public class ScrollViewActivity extends ActionBarActivity {

	private ImageView imageView;
	private TextView txtContext;
	private Drawable mDefaultImageDrawable;
	private TextToSpeech mSpeech;
	 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle data = getIntent().getExtras();
		String url = data.getString("url");
		String context = data.getString("context");
		String title = data.getString("title");

		getActionBar().setTitle(title);

		FadingActionBarHelper helper = new FadingActionBarHelper()
				.actionBarBackground(R.drawable.ab_background)
				.headerLayout(R.layout.header)
				.contentLayout(R.layout.activity_scrollview);
		setContentView(helper.createView(this));
		helper.initActionBar(this);

		findView();

		txtContext.setText(context);
		RequestManager.loadImage(HappyGyApi.BASE_URL + url, RequestManager
				.getImageListener(imageView, mDefaultImageDrawable,
						mDefaultImageDrawable));
	}

	public void findView() {
		imageView = (ImageView) findViewById(R.id.image_header);
		txtContext = (TextView) findViewById(R.id.txtContext);
		mDefaultImageDrawable = getResources().getDrawable(R.drawable.ny);
		
		mSpeech = new TextToSpeech(this, initListenr);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {

		super.onCreateContextMenu(menu, v, menuInfo);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();  
	    inflater.inflate(R.menu.speack_menu, menu); 
		return true;
	}

	public void textSpeak(MenuItem item) {
		if (item.getItemId() != R.id.action_speak)
			return;
		
		if (mSpeech.isSpeaking()) {
			mSpeech.stop();
		} else {
			mSpeech.speak(txtContext.getText().toString(), TextToSpeech.QUEUE_FLUSH,
	                null);
		}
	}

	
	OnInitListener initListenr = new OnInitListener() {
		public void onInit(int status) {
			if (status == TextToSpeech.SUCCESS) {
                int result = mSpeech.setLanguage(Locale.SIMPLIFIED_CHINESE);
                if (result != TextToSpeech.LANG_MISSING_DATA
                        && result != TextToSpeech.LANG_NOT_SUPPORTED) {
              
                } else {
                   
                }
            }
		}
	};
	
	@Override
    protected void onDestroy() {
        if (mSpeech != null) {
            mSpeech.stop();
            mSpeech.shutdown();
        }
        super.onDestroy();
    }
	
	
	
	
	
}
